package 设计二构造版本2;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 习题类，用于生成和管理加减法习题
 */
public class exercises {
    private List<fermula> formulaList; // 存储算式的列表
    private Random random;             // 随机数生成器
    
    /**
     * 构造函数
     */
    public exercises() {
        formulaList = new ArrayList<>();
        random = new Random();
    }
    
    /**
     * 构造函数，带随机种子参数
     * @param seed 随机种子
     */
    public exercises(long seed) {
        formulaList = new ArrayList<>();
        random = new Random(seed);
    }
    
    /**
     * 产生一定数量的随机习题（混合加法和减法）
     * @param count 要生成的习题数量
     */
    public void generateExercises(int count) {
        formulaList.clear(); // 清空现有习题
        
        for (int i = 0; i < count; i++) {
            fermula formula;
            // 随机决定是生成加法还是减法算式
            if (random.nextBoolean()) {
                formula = new AddFormula();
            } else {
                formula = new SubtractFormula();
            }
            formula.generate(random);
            formulaList.add(formula);
        }
    }
    
    /**
     * 产生一定数量的加法习题
     * @param count 要生成的加法习题数量
     */
    public void generateAddExercises(int count) {
        for (int i = 0; i < count; i++) {
            AddFormula addFormula = new AddFormula();
            addFormula.generate(random);
            formulaList.add(addFormula);
        }
    }
    
    /**
     * 产生一定数量的减法习题
     * @param count 要生成的减法习题数量
     */
    public void generateSubtractExercises(int count) {
        for (int i = 0; i < count; i++) {
            SubtractFormula subtractFormula = new SubtractFormula();
            subtractFormula.generate(random);
            formulaList.add(subtractFormula);
        }
    }
    
    /**
     * 格式化显示所有习题（不显示答案）
     */
    public void displayExercisesWithoutAnswers() {
        System.out.println("\n========== 习题集（不显示答案）==========\n");
        
        for (int i = 0; i < formulaList.size(); i++) {
            fermula formula = formulaList.get(i);
            System.out.printf("%2d. %d %c %d = ____\n", 
                            i + 1, 
                            formula.getLeftOperand(), 
                            formula.getOperator(), 
                            formula.getRightOperand());
            
            // 每5题换一行，提高可读性
            if ((i + 1) % 5 == 0) {
                System.out.println();
            }
        }
        System.out.println();
    }
    
    /**
     * 格式化显示所有习题（显示答案）
     */
    public void displayExercisesWithAnswers() {
        System.out.println("\n========== 习题集（显示答案）==========\n");
        
        for (int i = 0; i < formulaList.size(); i++) {
            fermula formula = formulaList.get(i);
            System.out.printf("%2d. %s\n", i + 1, formula.toString());
        }
        System.out.println();
    }
    
    /**
     * 按指定列数格式化显示习题（不显示答案）
     * @param columns 列数
     */
    public void displayExercisesInColumns(int columns) {
        System.out.println("\n========== 习题集（多列显示）==========\n");
        
        for (int i = 0; i < formulaList.size(); i += columns) {
            StringBuilder line = new StringBuilder();
            
            for (int j = 0; j < columns; j++) {
                int index = i + j;
                if (index < formulaList.size()) {
                    fermula formula = formulaList.get(index);
                    String problem = String.format("%2d. %d %c %d = ____  ", 
                                                index + 1, 
                                                formula.getLeftOperand(), 
                                                formula.getOperator(), 
                                                formula.getRightOperand());
                    line.append(problem);
                }
            }
            System.out.println(line.toString().trim());
        }
        System.out.println();
    }
    
    /**
     * 获取习题列表
     * @return 习题列表
     */
    public List<fermula> getFormulaList() {
        return new ArrayList<>(formulaList); // 返回副本，防止外部修改
    }
    
    /**
     * 获取习题数量
     * @return 习题数量
     */
    public int getExerciseCount() {
        return formulaList.size();
    }
    
    /**
     * 清空习题列表
     */
    public void clear() {
        formulaList.clear();
    }
    
    /**
     * 统计加法和减法习题的数量
     */
    public void statistics() {
        int addCount = 0;
        int subtractCount = 0;
        
        for (fermula formula : formulaList) {
            if (formula.getOperator() == '+') {
                addCount++;
            } else if (formula.getOperator() == '-') {
                subtractCount++;
            }
        }
        
        System.out.println("\n========== 习题统计 ==========\n");
        System.out.printf("总题数: %d\n", formulaList.size());
        System.out.printf("加法题: %d 题 (%.1f%%)\n", addCount, (float)addCount / formulaList.size() * 100);
        System.out.printf("减法题: %d 题 (%.1f%%)\n", subtractCount, (float)subtractCount / formulaList.size() * 100);
        System.out.println();
    }
}